package gspushmq.v1.subscribe.send.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.java_websocket.WebSocket;

import com.alibaba.fastjson.JSON;

import gspushmq.model.Data;
import gspushmq.model.Message;
import gspushmq.util.ToolsUtil;
import gspushmq.v1.save.SaveSendRecord;
import gspushmq.v1.thread.GGThreadPool;

public abstract class GGSend implements Comparable<Object>, Runnable {

	private Integer priority = 30;
	
	public abstract void handler();
	
	@Override
	public void run() {
		handler();
	}
	
	@Override
	public int compareTo(Object o) {
		Integer op = ToolsUtil.commonTo(o);
		return op.compareTo(this.getPriority());
	}
	
	private void writeLog(Message message, Data data) {
		SaveSendRecord.save(message, data);
	}
	
	/**
	 * 推送消息给客户端
	 * @param ams
	 * @param wst
	 * @param apiDataMessage
	 * @author yutao
	 * @date 2018年11月16日下午3:47:01
	 */
	public void sendWebSocket(Message ams, WebSocket wst, Data apiDataMessage) {
		//定位到用户订阅了这类消息
		Map<String, Object> result = new HashMap<>();
		//表示有效
		result.put("code", 0);
		result.put("request_id", ams.getRequest_id());
		result.put("uuid", apiDataMessage.getUuid());
		result.put("data", apiDataMessage.getContent());
		wst.send(JSON.toJSONString(result));
		//写日志
		writeLog(ams, apiDataMessage);
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}
}
